import { http } from '@/utils/request'
import type { ApiResponse } from '@/types'

export const adminApi = {
  getDashboardStats(): Promise<ApiResponse<{
    todayReviewed: number
    approvalRate: number
    pendingProjects: number
    pendingQuestions: number
    totalUsers: number
    totalQuestions: number
    totalContests: number
    todaySubmissions: number
    pendingAudits: number
  }>> {
    return http.get('/admin/dashboard/stats')
  },

  getRecentActivities(): Promise<ApiResponse<Array<{
    id: number
    description: string
    type: string
    created_at: string
  }>>> {
    return http.get('/admin/dashboard/activities')
  },

  getQuestions(params?: {
    status?: string
    difficulty?: string
    audit_status?: string
    page?: number
    page_size?: number
  }): Promise<ApiResponse<{
    questions: Array<{
      id: number
      title: string
      content: string
      difficulty: string
      status: string
      audit_status: string
      audit_reason?: string
      audited_at?: string
      author: {
        id: number
        username: string
      }
      tags: Array<{
        id: number
        name: string
        display_name: string
      }>
      created_at: string
    }>
    total: number
    page: number
    page_size: number
  }>> {
    return http.get('/admin/questions', { params })
  },

  getPendingQuestions(params?: {
    difficulty?: string
    page?: number
    page_size?: number
  }): Promise<ApiResponse<{
    questions: Array<{
      id: number
      title: string
      content: string
      difficulty: string
      audit_status: string
      author: {
        id: number
        username: string
      }
      tags: Array<{
        id: number
        name: string
        display_name: string
      }>
      created_at: string
    }>
    total: number
    page: number
    page_size: number
  }>> {
    return http.get('/admin/questions/pending', { params })
  },

  auditQuestion(questionId: number, action: 'approve' | 'reject', reason?: string): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/questions/${questionId}/audit`, {
      action,
      reason
    })
  },

  auditProject(projectId: number, action: 'approve' | 'reject', reason?: string): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/projects/${projectId}/audit`, {
      action,
      reason
    })
  },

  getPendingProjects(params?: {
    type?: string
    page?: number
    page_size?: number
  }): Promise<ApiResponse<{
    projects: Array<{
      id: number
      title: string
      description: string
      type: string
      status: string
      created_at: string
    }>
    total: number
    page: number
    page_size: number
  }>> {
    return http.get('/admin/projects', { params })
  },

  // 竞赛管理相关API
  getContests(params?: {
    status?: string
    type?: string
    search?: string
    page?: number
    page_size?: number
  }): Promise<ApiResponse<{
    contests: Array<{
      id: number
      title: string
      description?: string
      type: 'individual' | 'team'
      status: 'upcoming' | 'running' | 'ended'
      start_time: string
      end_time: string
      duration: number
      max_participants?: number
      participant_count: number
      is_public: boolean
      author_id: number
      author?: {
        id: number
        username: string
        avatar?: string
      }
      created_at: string
      updated_at: string
    }>
    total: number
    page: number
    page_size: number
  }>> {
    return http.get('/admin/contests', { params })
  },

  createContest(data: {
    title: string
    description?: string
    type: 'individual' | 'team'
    start_time: string
    end_time: string
    duration: string
    max_participants?: number
    is_public: boolean
    rule?: string
  }): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post('/admin/contests/creat', data)
  },

  updateContest(id: number, data: {
    title?: string
    description?: string
    type?: 'individual' | 'team'
    start_time?: string
    end_time?: string
    duration?: number
    max_participants?: number
    is_public?: boolean
    rules?: string
  }): Promise<ApiResponse<{
    message: string
  }>> {
    return http.put(`/admin/contests/${id}`, data)
  },

  // 删除竞赛
  deleteContest(id: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.delete(`/admin/contests/${id}/delete`)
  },

  // 审核竞赛
  auditContest(id: number, data: {
    action: 'approve' | 'reject'
    reason?: string
  }): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/contests/${id}/audit`, data)
  },

  // 为竞赛添加题目
  addContestQuestions(id: number, data: {
    question_ids: number[]
  }): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/contests/${id}/questions`, data)
  },


  getContest(id: number): Promise<ApiResponse<{
    id: number
    title: string
    description?: string
    type: 'individual' | 'team'
    status: 'upcoming' | 'running' | 'ended'
    start_time: string
    end_time: string
    duration: number
    max_participants?: number
    participant_count: number
    is_public: boolean
    rules?: string
    author_id: number
    author?: {
      id: number
      username: string
      avatar?: string
    }
    questions?: Array<{
      id: number
      title: string
      difficulty: string
    }>
    created_at: string
    updated_at: string
  }>> {
    return http.get(`/admin/contests/${id}`)
  },

  // 题目状态管理API
  publishQuestion(questionId: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/questions/${questionId}/audit`, {
      action: 'approve'
    })
  },

  unpublishQuestion(questionId: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/admin/questions/${questionId}/audit`, {
      action: 'reject'
    })
  },

  deleteQuestion(questionId: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.delete(`/admin/questions/${questionId}/delete`)
  }
}